<?php
/**
 * Class: ratecourse
 * @author ovo
 * This class provides functionalities for rating a particular course.
 */
class Community_Model_ratecourse extends Zend_Db_Table_Abstract {
	
	protected $_name = 'course_rating';
	protected $_db;
		
	public function __construct(){
		
		$this->_db = Zend_Db_Table_Abstract::getDefaultAdapter();
	}
	/**
	 * Function: addRating
	 * This function adds a new users rating to the system.
	 * Each user is only allowed to rate a particular instance of a course once.
	 * @param unknown_type $diff
	 * @param unknown_type $text
	 * @param unknown_type $ins
	 * @param unknown_type $time
	 * @param unknown_type $grade
	 * @param unknown_type $comments
	 * @param unknown_type $uid
	 * @param unknown_type $cid
	 * @param unknown_type $year
	 * @param unknown_type $semester
	 */
	public function addRating($diff, $text, $ins, $time, $grade, $comments,$uid, $cid, $year, $semester){	
	
		$data = array (
		'usefulness_textbook' => $text,
		'difficulty' => $diff,
		'instructor_rating' => $ins,
		'timespent' => $time,
		'final_grade' => $grade,
		'comments' => $comments,
		'user_id' => $uid,
		'semester' => $semester,
		'year' => $year,
		'course_id' => $cid,
		'date_entered' => new Zend_Db_Expr("now()")
		);
		
		$this->insert($data);
	}
	


	/**
	 * Function: viewComments
	 * This function allows a user view all the comments
	 * of a particular cours
	 * @param $courseid, $semester, $year
	 */
	
	public function viewComments($courseid, $semester, $year){
		
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$select = new Zend_Db_Select($this->_db);
		$result = $select->from(array('c'=>'course_rating'), array('id','comments', 'date_entered'))
						 ->join(array('u'=>'users'), 'c.user_id=u.id', array('username'))
					  ->where('c.course_id ='. $courseid)
					  ->where('c.semester ='. $this->_db->quote($semester))
					  ->where('c.year ='. $year)
					  ->order(array('date_entered DESC'));
	 // Zend_Debug::dump($result->__toString());	
				  
	 return $result;
	}
	
	/**
	 * Function: delRating
	 * This function deletes all ratings associated with a particular post.
	 * @param unknown_type $id
	 */
	public function delRating($id) {
		
		$this->delete('id ='. $id);
	}
	
	/**
	 * Function: avgRatings
	 * This function returns the average ratings of a particular course
	 * @param unknown_type $courseid
	 * @param unknown_type $semester
	 * @param unknown_type $year
	 */
	public function avgRatings($courseid, $semester, $year){
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$semester = $this->_db->quote($semester);
		$select = new Zend_Db_Select($this->_db);	
		$result = $select->from(array ('c' =>'course_rating'), array(
		'time'=>'avg(timespent)', 'recount' => 'count(id)',
		'final'=>'round(avg(final_grade))', 'i_rating'=>'avg(instructor_rating)', 
		'use_rating'=>'avg(usefulness_textbook)', 'diff'=>'avg(difficulty)'))
			->where('c.course_id ='. $courseid)
			->where(' c.semester = '. $semester )
			->where(' c.year ='. $year);
		//Zend_Debug::dump($result->__toString());
		return $result;	
	}
	
	/*
	 * Function: getDistribution
	 * This function returns the number of respondents that found this class to difficult at a given level
	 * @ params $courseid, $semester, $year, $level
	 */
	public function getDistribution($courseid, $semester, $year, $level){
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$select = new Zend_Db_Select($this->_db);
		$semester = $this->_db->quote($semester);
		$diff = array(); //An array to store all the difficulty distribution
		$result = $select->from(array('course_rating'), array('diffcount' =>'count(difficulty)')) 
						->where('course_id ='. $courseid)
						->where('semester = '. $semester )
						->where('year ='. $year)
						->where('difficulty = '. $level);
		$ff = $this->_db->fetchAll($result);
		//echo $result->__toString();
		$vals = $ff[0]['diffcount'];
		return $vals;	
		}
		
	public function getTimeDistribution($courseid, $semester, $year, $level){
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$select = new Zend_Db_Select($this->_db);
		$semester = $this->_db->quote($semester);
		$diff = array(); //An array to store all the difficulty distribution
		$result = $select->from(array('course_rating'), array('val' =>'count(timespent)')) 
						->where('course_id ='. $courseid)
						->where('semester = '. $semester )
						->where('year ='. $year)
						->where('timespent = '. $level);
		$ff = $this->_db->fetchAll($result);
		//echo $result->__toString();
		$vals = $ff[0]['val'];
		return $vals;	
		}

	public function getInstructorDistribution($courseid, $semester, $year, $level){
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$select = new Zend_Db_Select($this->_db);
		$semester = $this->_db->quote($semester);
		$diff = array();				 //An array to store all the difficulty distribution
		$result = $select->from(array('course_rating'), array('val' =>'count(instructor_rating)')) 
						->where('course_id ='. $courseid)
						->where('semester = '. $semester )
						->where('year ='. $year)
						->where('instructor_rating = '. $level);
		$ff = $this->_db->fetchAll($result);
		//echo $result->__toString();
		$vals = $ff[0]['val'];
		return $vals;	
		}
		public function getTextDistribution($courseid, $semester, $year, $level){
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$select = new Zend_Db_Select($this->_db);
		$semester = $this->_db->quote($semester);
		$diff = array();				 //An array to store all the difficulty distribution
		$result = $select->from(array('course_rating'), array('val' =>'count(usefulness_textbook)')) 
						->where('course_id ='. $courseid)
						->where('semester = '. $semester )
						->where('year ='. $year)
						->where('usefulness_textbook = '. $level);
		$ff = $this->_db->fetchAll($result);
		//echo $result->__toString();
		$vals = $ff[0]['val'];
		return $vals;	
		}

		public function getGradeDistribution($courseid, $semester, $year, $level){
		//$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$select = new Zend_Db_Select($this->_db);
		$semester = $this->_db->quote($semester);
		$diff = array();				 //An array to store all the difficulty distribution
		$result = $select->from(array('course_rating'), array('val' =>'count(final_grade)')) 
						->where('course_id ='. $courseid)
						->where('semester = '. $semester )
						->where('year ='. $year)
						->where('final_grade = '. $level);
		$ff = $this->_db->fetchAll($result);
		//echo $result->__toString();
		$vals = $ff[0]['val'];
		return $vals;	
		}
		
		/**
		 * Function: checkRecord
		 * This function prevents the user from duplicating an action that is only supposed to be carried out once.
		 * @param unknown_type $cid
		 * @param unknown_type $uid
		 * @param unknown_type $semester
		 * @param unknown_type $year
		 */
		public function checkRecord($cid, $uid, $semester, $year){
		
		$select = new Zend_Db_Select($this->_db);
		$semester = $this->_db->quote($semester);
		$result = $select->from(array('course_rating'), array('val' =>'count(id)')) 
						->where('course_id ='. $cid)
						->where('semester = '. $semester )
						->where('year ='. $year)
						->where('user_id ='. $uid);
		$ff = $this->_db->fetchAll($result);
		return $ff;
		}
		
	}
	